package juanma;

import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Font;

import javax.swing.JCheckBox;
import javax.swing.JTextPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;


public class WinAgregarItem extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textNombre;
	private JTextField textPrecio;
	private JLabel lblPeso;
	private JTextField textPeso;
	private JButton btnAgregar;
	private JButton btnAgregarYSeguir;
	private JCheckBox chkboxMagico;
	private JCheckBox chkboxEquipar;
	private JTextPane txtpnNotas;
	private JTextPane txtpnMagia;
	private JSpinner spinCantidad;
	
	public WinAgregarItem(final Backpack backpack, final JTable tableItems) {
		setTitle("Item >> " + backpack.nombre);
		setBounds(100, 100, 305, 314);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNombre = new JLabel("NOMBRE");
		lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblNombre.setBounds(10, 11, 102, 14);
		contentPane.add(lblNombre);
		
		textNombre = new JTextField();
		textNombre.setBounds(10, 23, 130, 20);
		contentPane.add(textNombre);
		textNombre.setColumns(10);
		textNombre.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 10){
					btnAgregarYSeguir.doClick();
				}
			}
		});
		
		textPrecio = new JTextField();
		textPrecio.setBounds(151, 118, 46, 20);
		contentPane.add(textPrecio);
		textPrecio.setColumns(10);
		textPrecio.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 10)
					btnAgregarYSeguir.doClick();
			}
		});
		
		textPeso = new JTextField();
		textPeso.setBounds(207, 118, 46, 20);
		contentPane.add(textPeso);
		textPeso.setColumns(10);
		textPeso.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 10)
					btnAgregarYSeguir.doClick();
				if(tecla.getKeyChar() == 9)
					spinCantidad.requestFocus();
			}
		});
		
		JLabel lblPrecio = new JLabel("PRECIO");
		lblPrecio.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblPrecio.setBounds(151, 105, 46, 14);
		contentPane.add(lblPrecio);
	
		lblPeso = new JLabel("PESO");
		lblPeso.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblPeso.setBounds(207, 105, 46, 14);
		contentPane.add(lblPeso);
		
		btnAgregar = new JButton("Agregar y Salir");
		btnAgregar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textNombre.getText() != ""){
					agregarItemA(backpack, tableItems);
					dispose();
				}
			}
		});
		btnAgregar.setBounds(150, 244, 130, 23);
		contentPane.add(btnAgregar);
		
		btnAgregarYSeguir = new JButton("Agregar y Seguir");
		btnAgregarYSeguir.setBounds(10, 244, 130, 23);
		contentPane.add(btnAgregarYSeguir);
		
		chkboxMagico = new JCheckBox("M\u00E1gico");
		chkboxMagico.setBounds(156, 22, 97, 23);
		contentPane.add(chkboxMagico);
		
		chkboxEquipar = new JCheckBox("Equipar");
		chkboxEquipar.setBounds(156, 48, 97, 23);
		contentPane.add(chkboxEquipar);
		
		txtpnNotas = new JTextPane();
		txtpnNotas.setBounds(10, 71, 130, 67);
		contentPane.add(txtpnNotas);
		txtpnNotas.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 9){
					String textoNotas = txtpnNotas.getText();
					txtpnNotas.setText(textoNotas.substring(0, textoNotas.length()-1));
					txtpnMagia.requestFocus();
				}
			}
		});
		
		JLabel lblNotas = new JLabel("NOTAS");
		lblNotas.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblNotas.setBounds(10, 57, 102, 14);
		contentPane.add(lblNotas);
		
		txtpnMagia = new JTextPane();
		txtpnMagia.setBounds(10, 162, 130, 67);
		contentPane.add(txtpnMagia);
		txtpnMagia.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 9){
					String textoMagia = txtpnMagia.getText();
					txtpnMagia.setText(textoMagia.substring(0, textoMagia.length()-1));
					textPrecio.requestFocus();
				}
			}
		});
		
		JLabel lblNotasMagicas = new JLabel("MAGIA");
		lblNotasMagicas.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblNotasMagicas.setBounds(10, 149, 102, 14);
		contentPane.add(lblNotasMagicas);
		
		spinCantidad = new JSpinner();
		spinCantidad.setFont(new Font("Tahoma", Font.PLAIN, 10));
		spinCantidad.setModel(new SpinnerNumberModel(new Integer(1), new Integer(0), null, new Integer(1)));
		spinCantidad.setBounds(151, 162, 46, 20);
		contentPane.add(spinCantidad);
		spinCantidad.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
			}
			@Override
			public void keyTyped(KeyEvent tecla) {
				if(tecla.getKeyChar() == 10)
					btnAgregarYSeguir.doClick();
			}
		});
		
		JLabel lblCantidad = new JLabel("CANTIDAD");
		lblCantidad.setFont(new Font("Tahoma", Font.PLAIN, 10));
		lblCantidad.setBounds(151, 149, 63, 14);
		contentPane.add(lblCantidad);
		
		btnAgregarYSeguir.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				if(textNombre.getText() != ""){
					agregarItemA(backpack, tableItems);
					textNombre.setText("");
					textPeso.setText("");
					textPrecio.setText("");
					txtpnNotas.setText("");
					txtpnMagia.setText("");
					chkboxMagico.setSelected(false);
					chkboxEquipar.setSelected(false);
					spinCantidad.setValue(1);
				}
			}
		});
	}
	
	public static void main(final Backpack backpack, final JTable tableItems) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WinAgregarItem frame = new WinAgregarItem(backpack, tableItems);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private Item crearItem(){
		Item itemNuevo = new Item();
		itemNuevo.nombre = textNombre.getText();
		if(!textPeso.getText().matches(""))
			itemNuevo.setPeso(Double.parseDouble(textPeso.getText()));
		if(!textPrecio.getText().matches(""))
			itemNuevo.setPrecio(Double.parseDouble(textPrecio.getText()));
		itemNuevo.notas = txtpnNotas.getText();
		itemNuevo.equipado = chkboxEquipar.isSelected();
		itemNuevo.magico = chkboxMagico.isSelected();
		itemNuevo.magia = txtpnMagia.getText();
		itemNuevo.setCantidad((Integer)spinCantidad.getValue());
		return itemNuevo;
	}
	
	private void agregarItemA(Backpack backpack, JTable tablaItems){
		Item itemNuevo = crearItem();
		backpack.add(itemNuevo);
		DefaultTableModel tm = (DefaultTableModel)tablaItems.getModel();
		String[] datosNuevos = {String.valueOf(itemNuevo.getCantidad()), itemNuevo.nombre};
		tm.addRow(datosNuevos);
	}
}
